﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data.Linq
Imports System.Text
Imports DevComponents
Imports System.Data
Imports DevComponents.DotNetBar
Imports TNCDAL
Imports HTLibrary
Imports System.Xml

Public Class ucGoods


    Dim mgo As New MGoods
    Dim ego As New tblGood
    Dim TotRowCount As Integer
    Dim RowId As Integer
    Dim mg As New MGoods
    Dim isNew As Boolean = False
    Dim isEdit As Boolean = False
    Dim indexRow As Integer = 0
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DataGridViewHelper.DataGridView_InitEvent(grid)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub bindingTogrid()
        Dim query = mg.getGoodsList3()
        Dim source As New BindingSource
        source.DataSource = query
        grid.DataSource = source
        BindingNavigator1.BindingSource = source
        grid.Columns(0).Visible = False
        grid.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        Try
            grid.CurrentCell.Selected = False

        Catch ex As Exception

        End Try
    End Sub
    Sub bindingTogridSearch()
        Dim query = mg.getGoodsList3(txtGoodsNameSearch.Text)
        Dim source As New BindingSource
        source.DataSource = query
        grid.DataSource = source
        BindingNavigator1.BindingSource = source
        grid.Columns(0).Visible = False
        grid.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        Try
            grid.CurrentCell.Selected = False

        Catch ex As Exception

        End Try
    End Sub
    Private Sub EnableControls(ByVal v As Boolean)
        txtDescription.Enabled = v
        txtGoodName.Enabled = v
        btnSave.Enabled = v
        btnCancel.Enabled = v
        If isNew Then
            txtDescription.Text = ""
            txtGoodName.Text = ""
            txtGoodName.Focus()
        End If

    End Sub
    

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If (MessageBox.Show("Do you really want to delete '" & txtGoodName.Text & "' record ?",
                       "Delete Warning", MessageBoxButtons.YesNo,
                       MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, False) = DialogResult.Yes) Then

            'For i As Integer = 0 To grid.SelectedRows.Count - 1
            '    RowId = grid.SelectedRows(i).Index
            '    ego.ID_Good = Convert.ToInt32(grid(0, RowId).Value)

            '    If ego.ID_Good <> 0 Then
            '        mgo.DeleteRecord(ego.ID_Good)
            '    End If

            'Next

            Dim id As Integer = CInt(txtIDGood.Text)

            If id <> 0 Then
                mg.DeleteUpdateRecord(id)
            End If


        End If
        EnableControls(False)
        bindingTogrid()
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
    End Sub

    Private Sub FrmGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Bar1.Text = "Search"
        Bar2.Text = "Details"
        bindingTogrid()
        EnableControls(False)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        isNew = False
        isEdit = False
        EnableControls(False)
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
        grid.Enabled = True
    End Sub

 

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        isNew = True
        isEdit = False
        EnableControls(True)
        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        grid.Enabled = False
    End Sub

    
     

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If isNew Then
            If txtGoodName.Text = "" Then
                MessageBox.Show("Chưa nhập tên hàng hóa")
                Return
            End If
            Dim obj As New tblGood
            obj.GoodName = txtGoodName.Text

            If txtDescription.Text <> "" Then
                obj.Description = txtDescription.Text
            End If
            obj.IsActive = True
            mg.InsertNewRecord(obj)
            isNew = False
            isEdit = False
            bindingTogrid()
            EnableControls(False)
        ElseIf isEdit Then

            If txtGoodName.Text = "" Then
                MessageBox.Show("Chưa nhập tên hàng hóa")
                Return
            End If

            Dim obj As New tblGood
            obj.ID_Good = CInt(txtIDGood.Text)
            obj.GoodName = txtGoodName.Text

            If txtDescription.Text <> "" Then
                obj.Description = txtDescription.Text
            End If
            mg.UpdateRecord(obj)
            isNew = False
            isEdit = False
            bindingTogrid()
            EnableControls(False)
            grid.Rows(indexRow).Selected = True
            grid.FirstDisplayedScrollingRowIndex = indexRow
            grid.CurrentCell = grid(1, indexRow)
        End If
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
        grid.Enabled = True

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        isNew = False
        isEdit = True
        EnableControls(True)
        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        grid.Enabled = False
        txtGoodName.Focus()
        indexRow = grid.CurrentRow.Index
    End Sub

    Private Sub grid_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grid.RowEnter
        Dim rw As Integer = e.RowIndex
        Dim dg As DataGridView = CType(sender, DataGridView)
        txtGoodName.Text = dg.Item(1, rw).Value   'row.Item("id").ToString
        txtDescription.Text = dg.Item(2, rw).Value 'row.Item("caption").ToString
        txtIDGood.Text = dg.Item(0, rw).Value 'row.Item("menu_desc").ToString
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        bindingTogridSearch()
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click

    End Sub

    Private Sub txtGoodsNameSearch_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtGoodsNameSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            bindingTogridSearch()
        End If
    End Sub
End Class